﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Gds.Model.ConfigModel.Controller;
using Gds.Model.ConfigModel.GasMeter;
using Gds.Model.ConfigModel.SerialPort;
namespace Gds.ViewModel.pageViewModel
{
    public class SystemConfigViewModel : INotifyPropertyChanged
    {
        #region defaultParam
        public static volatile SystemConfigViewModel _instance = new SystemConfigViewModel();
        public static object LockHelper = new object();
        public SystemConfigViewModel Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (LockHelper)
                    {
                        if (_instance == null)
                        {
                            _instance = new SystemConfigViewModel();
                        }
                    }
                }
                return _instance;
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
        #region Public Param Config
        private ObservableCollection<ControllerConfigModel> _controllerModels;
        private ObservableCollection<GasMeterConfig> _gasMeterModels;
        private ObservableCollection<EquipSerialPortModel> _equipPortModels;
        private ObservableCollection<EquipSerialPortModel> _controllerPortModels;
        private ObservableCollection<EquipSerialPortModel> _relayPortModels;

        /// <summary>
        /// 控制器配置模型
        /// </summary>
        public ObservableCollection<ControllerConfigModel> ControlerModels
        {
            get { return _controllerModels; }
            set { _controllerModels = value; OnPropertyChanged("ControlerModels"); }
        }
        /// <summary>
        /// 通讯端口配置模型
        /// </summary>
        public ObservableCollection<EquipSerialPortModel> EquipPortModels
        {
            get { return _equipPortModels; }
            set { _equipPortModels = value; OnPropertyChanged("EquipPortModels"); }
        }
        /// <summary>
        /// 控制器通讯端口
        /// </summary>
        public ObservableCollection<EquipSerialPortModel> ControllerPortModels
        {
            get
            {
                _controllerPortModels = new ObservableCollection<EquipSerialPortModel>();
                foreach (var k in EquipPortModels)
                {
                    if (k.ConfigType == "控制器")
                    {
                        _controllerPortModels.Add(k);
                    }
                }
                return _controllerPortModels;
            }
        }
        /// <summary>
        /// 继电器通讯端口
        /// </summary>
        public ObservableCollection<EquipSerialPortModel> RelayPortModels
        {
            get
            {
                _relayPortModels = new ObservableCollection<EquipSerialPortModel>();
                foreach (var k in EquipPortModels)
                {
                    if (k.ConfigType != "控制器")
                    {
                        _relayPortModels.Add(k);
                    }
                }
                return _relayPortModels;
            }
        }
        /// <summary>
        /// 气体探测器配置模型
        /// </summary>
        public ObservableCollection<GasMeterConfig> GasMeterModels
        {
            get { return _gasMeterModels; }
            set { _gasMeterModels = value; OnPropertyChanged("GasMeterModels"); }
        }
        #endregion
        #region LoadConfig
        /// <summary>
        /// 加载所有配置
        /// </summary>
        public void LoadAllConfig()
        {
            //加载控制器配合
            ControlerModels = LoadDBColConfit<ControllerConfigModel>("ControllerModelList");
            //加载探测器配合
            GasMeterModels = LoadDBColConfit<GasMeterConfig>("GasMeterConfigList");
            //加载通讯端口配合
            EquipPortModels = LoadDBColConfit<EquipSerialPortModel>("EquipPortConfig");
        }
        #region private Function
        private ObservableCollection<T> LoadDBColConfit<T>(string dbName) where T : class, new()
        {
            var result = new ObservableCollection<T>();
            result = Utility.Helper.DataBaseHelper.FindModelCol<T>(dbName);
            return result;
        }
        #endregion 
        #endregion
    }
}
